<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Inter\Controller;
use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class UserController extends InterController {
   function __construct() {
	   parent::__construct();
      $this->mem = D("Member");
	  $this->uc = D("ucenter_member");
	  $this->upimg = D("File");
   }
	/* 用户中心首页 */
	public function index(){
		echo '44';
	}
	/* 验证码登录  */
	public function Vlogin(){
		$status['code'] = 1;
		$mobile = $_POST['mobile'];
		        if($mobile=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
	    $rs = $this->uc->field('id')->where(array('mobile'=>$mobile))->find();

	    if($rs['id']>0)
		{
			$data = $this->mem->where(array('uid'=>$rs['id']))->find();

			if($data['uid']>0)
			{
				     $data['imgurl'] = get_cover($data['cover_id']);
                     $status['code'] = 0;
					 $data['mobile'] = $mobile;
                     $status['data'] = $data;
                     $this->ajaxReturn($status);
			}

		}else
		{    $password = md5(time());
            $User = new UserApi;
			$time=time();
            $ucdata= array('username'=>$mobile,'mobile'=>$mobile,'regtime'=>$time,'status'=>1,'password'=>$password);
            $uid = $this->uc->add($ucdata);
			if(0 < $uid){ 
				//没登录 插入数据
				$Member = D('Member');
              	if($Member->login($uid)){
					 $status['code'] = 0;
					 $userinfo = $this->mem->where(array('uid'=>$uid))->find();
					 $userinfo['mobile'] = $mobile;
                     $status['data'] = $userinfo;
                     $this->ajaxReturn($status);
				} else {

					 $status['msg'] = $Member->getError();
                     $this->ajaxReturn($status);
				}
			} else { 
				     $status['code'] = 0;
                     $status['msg'] = $this->showRegError($uid);
                     $this->ajaxReturn($status);
			}
		}
		$status['msg'] = '登录失败';
        $this->ajaxReturn($status);
	}
	/* 第三方登录是否绑定手机*/
    public function Checkphone(){
        $weixin = $_POST['weixin'];
		$qq = $_POST['qq'];

		$map = array();
		if($qq==''&& $weixin=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$map=array();
		if($weixin)
		{
			$map['weixin_auth'] = $weixin;

		}
		if($qq)
		{
			$map['qq_auth'] = $qq;
		}
		$rs = $this->uc->where($map)->find();
		$info = $this->mem->where(array('uid'=>$rs['id']))->find();
		if($rs['id']>0)
		{
			$info['mobile'] = $rs['mobile'];
                              $status['code'] = 0;
                               $status['data'] = $info;
							  $status['msg'] = '手机已经绑定';
                    $this->ajaxReturn($status);
		}else{
		                              $status['code'] = 1;
									  $status['msg'] = '未绑定手机';
                    $this->ajaxReturn($status);
		}
	}
	/* 密码登录*/
	public function Plogin(){
		$status['code'] = 1;
		$mobile = $_POST['mobile'];
		$password = $_POST['Password'];
		        if($mobile=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		        if($password=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
	    	$user = new UserApi;
			$uid = $user->login($mobile, $password,'3');
			if(0 < $uid){ //UC登录成功
				/* 登录用户 */
				$Member = D('Member');
				if($Member->login($uid)){ 
			
					$rs = $this->mem->where(array('uid'=>$uid))->find();
				//	$rs['imgurl'] = get_cover($rs['cover_id']);
					 $status['code'] = 0;
					  $rs['mobile'] = $mobile;
                     $status['data'] = $rs;
                     $this->ajaxReturn($status);
				} else {
	
					     $status['msg'] = $Member->getError();
                          $this->ajaxReturn($status);
				}

			}else
		    {
								switch($uid) {
					case -1: $error = '用户不存在！'; break; //系统级别禁用
					case -2: $error = '密码错误！'; break;
					default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
				}
                   $status['msg'] = $error;
                    $this->ajaxReturn($status);
		    }



	}


	//手机绑定
	public function Bindphone()
	{
		$mobile = $_POST['mobile'];
        $weixin = $_POST['weixin'];
		$qq = $_POST['qq'];
		$nickname = $_POST['nickname'];
		$avatar = $_POST['avatar'];
		$map = array();
		if($mobile=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$map=array();
		if($weixin)
		{
			$map['weixin_auth'] = $weixin;

		}
		if($qq)
		{
			$map['qq_auth'] = $qq;
		}
	   $rs = $this->uc->where(array('mobile'=>$mobile))->find();
	   if($rs['id']>0)
		{   $info = $this->mem->where(array('uid'=>$rs['id']))->find();
	        if(!$info['nickname'])
			{
               $data['nickname'] = $nickname;
			   $info['nickname'] = $nickname;
			}
			if(!$info['thumb'])
			{
               $data['thumb'] = $avatar;
			   $info['thumb'] = $avatar;
			}
            $this->uc->where(array('mobile'=>$mobile))->save($map);
			$this->mem->where(array('uid'=>$rs['id']))->save($data);
            	$status['code'] = 0;
				$info['mobile'] = $mobile;
				$status['data'] =  $info;
				$this->ajaxReturn($status);
		}else
		{
		   $time = time();
		   $map['mobile'] = $mobile;
		   $map['username'] = $mobile;
		   $map['reg_time'] = $time;
		   $map['status'] = 1;
          $uid = $this->uc->add($map);
		      $data['nickname'] = $nickname;
			  $data['thumb'] = $avatar;
			   $data['status'] = 1;
			   $data['reg_time'] = $time;
			    $data['login'] = 1;
				$data['uid'] = $uid; 
		   $this->mem->add($data);
		   		$status['code'] = 0;
				$data['mobile'] = $mobile;
        $status['data'] =  $data;
        $this->ajaxReturn($status);
		}

	}
    /**
     * 修改头像 昵称
     * 
     */
    public function Uava(){
            $status['code'] = 1;
            //获取参数
            $uid   =   $_POST['uid'];
            $nickname   =   $_POST['nickname'];
		if($uid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		if($nickname)
		{
          $this->mem->where(array('uid'=>$uid))->save(array('nickname'=>$nickname));
		  	$status['code'] = 0;
		$this->ajaxReturn($status);
		}
		if(!empty($_FILES)){
			  $info = $this->upimg->upload($_FILES);
			  $info = $info['avatar']; 
			  if(is_array($info))
			{
				 
				              $data['thumb'] =  '/Uploads/'.$info['path'];
							  $data['cover_id'] = $info['id'];
							  $this->mem->where(array('uid'=>$uid))->save($data);
							  
							  $info['path'] = '/Uploads/'.$info['path'];
					$status['code'] = 0;
				   $status['data'] = $info;
				   $this->ajaxReturn($status);

			}else{
				 
				   $status['msg'] = $info;
				   $this->ajaxReturn($status);
			}             
	    }
		$status['msg'] = '无效操作';
		$this->ajaxReturn($status);
    }
	    /**
     * 手机换绑定
     * 
     */
    public function ChangeM(){
            $status['code'] = 1;
            //获取参数
            $uid   =   $_POST['uid'];
			$mobile   =   $_POST['mobile'];

		if($uid=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
				if($mobile=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
          $count = $this->uc->where(array('mobile'=>$mobile))->count();
		  if($count>0)
		{
			$status['msg'] = '手机已绑定';
            $this->ajaxReturn($status);
		}else
		{
            $this->uc->where(array('id'=>$uid))->save(array('mobile'=>$mobile,'username'=>$mobile));
			            $status['code'] = 0;
            $this->ajaxReturn($status);
		}            

    }
    /**
     * 修改密码
     * 
     */
    public function profile(){
$status['code'] = 1;
            //获取参数
            $mobile   =   $_POST['mobile'];
            $password = $_POST['Password'];
					        if($mobile=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		        if($password=='')
		{
		
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
			$data['password'] = $password;
            $Api = new UserApi();
            $res = $Api->updateM($mobile, $password, $data);
            if($res['status']){
                            $status['code'] = 0;
                           $this->ajaxReturn($status);
            }else{
         
				            $status['msg'] = $res['info'];
                   $this->ajaxReturn($status);
            }

    }

}
